package com.zyd.shiro.business.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallOrderAddressDao;
import com.zyd.shiro.persistence.beans.OrderAddress;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallOrderAddressService {

	@Autowired
	private MallOrderAddressDao orderAddressDao;
	
	

	public List<OrderAddress> findAll() {

		return orderAddressDao.findAll();
	}
	public List<OrderAddress> allRegionByRegionId(Integer regionId)
	{
		return orderAddressDao.allRegionByRegionId(regionId);
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<OrderAddress> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return orderAddressDao.findAll(pageRequest);
	}

	private Specification<OrderAddress> where(Map searchMap) {
		
		return new Specification<OrderAddress>() {
          
			@Override
			public Predicate toPredicate(Root<OrderAddress> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
                	predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
                }
                // 
                if (searchMap.get("phone")!=null && !"".equals(searchMap.get("phone"))) {
                	predicateList.add(cb.like(root.get("phone").as(String.class), "%"+(String)searchMap.get("phone")+"%"));
                }
                // 
                if (searchMap.get("detail")!=null && !"".equals(searchMap.get("detail"))) {
                	predicateList.add(cb.like(root.get("detail").as(String.class), "%"+(String)searchMap.get("detail")+"%"));
                }
				if (searchMap.get("region_id") != null && !"".equals(searchMap.get("region_id"))) {
					predicateList.add(cb.equal(root.get("region_id").as(Integer.class), (Integer) searchMap.get("region_id") ));
				}

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<OrderAddress> findSearch(int regionId, int page, int size) {
		Map whereMap = new HashMap<>();
		whereMap.put("region_id",regionId);
		Specification<OrderAddress> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return orderAddressDao.findAll(specification, pageRequest);
	}

	public OrderAddress findOneByOrderid(Integer orderid){
		return orderAddressDao.findByOrder_id(orderid);
	}
	public OrderAddress findOne(Integer id) {
		return orderAddressDao.findOne(id);
	}

	public void add(OrderAddress orderAddress) {
		
		orderAddressDao.save(orderAddress);
	}
	
	public void update(OrderAddress orderAddress) {
		orderAddressDao.save(orderAddress);
	}

	public void delete(Integer id) {
		orderAddressDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			orderAddressDao.delete(id);
		}
	}

}
